package com.findfuture.demo.service.impl;

import com.findfuture.common.dao.HibernateDaoImpl;
import com.findfuture.demo.model.Users;
import com.findfuture.demo.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 11-11-20
 * Time: 下午11:53
 * To change this template use File | Settings | File Templates.
 */
@Service("usersService")
public class UsersServiceImpl implements IUsersService {


    @Autowired
    private HibernateDaoImpl commonDao;

    @Override
    public Users getUserById(int id) throws Exception {
        commonDao.setEntryClass(Users.class);
        Users users = (Users) commonDao.fetch(id);
        return users;
    }

    @Override
    public Users getUserByName(String name) throws Exception {
        commonDao.setEntryClass(Users.class);
        List list = commonDao.query("from " + commonDao.getEntryClass().getSimpleName() + " where userName = ?", name);
        if (list != null && list.size() > 0) {
            return (Users) list.get(0);
        }
        return null;
    }

    @Override
    public List<Users> getUsersList() throws Exception {
        commonDao.setEntryClass(Users.class);
        return commonDao.query("from " + commonDao.getEntryClass().getSimpleName() + " where state = 0");
    }

    @Override
    public List<Users> getUsersListByWhere(String sqlWhere) throws Exception {
        commonDao.setEntryClass(Users.class);
        return commonDao.query("from " + commonDao.getEntryClass().getSimpleName() + " where state = 0 " + sqlWhere);
    }

    @Override
    public void addOrUpdate(Users user) throws Exception {
        commonDao.setEntryClass(Users.class);
        commonDao.getHibernateTemplate().saveOrUpdate(user);
    }

    @Override
    public void deleteUser(int userId) throws Exception {
        commonDao.executeSql("delete from users where id = ?" , userId);
    }

    @Override
    public void addUserRelation(int userId, int relationId) throws Exception {
        commonDao.executeSql("delete from users_relation where userId = ?", userId);
        commonDao.executeSql(" insert into users_relation values(?,?)", userId, relationId);
    }

    @Override
    public int getRelationUserId(int userId) throws Exception {
        int id = 0;
        List list = commonDao.querySql("select relationId from users_relation where userId = ?", userId);
        if (list != null && list.size() > 0) {
            id = (Integer) list.get(0);
        }
        return id;
    }

    @Override
    public List<Users> getUsersByIds(String ids) {
        commonDao.setEntryClass(Users.class);
        return commonDao.query(" from " + Users.class.getSimpleName() + " where id in ( " + ids + ")");
    }

    @Override
    public List<Users> getUserChild(int userId) throws Exception {
        List list = commonDao.querySql("select getChildLst(?)", userId);
        List<Users> users = null;
        if (list != null && list.size() > 0) {
            String value = String.valueOf(list.get(0));
            if (value.split(",").length != 2) {
                String replaceStr = "$," + userId + ",";
                value = value.replace(replaceStr, "");
                users = getUsersByIds(value);
            }
        }
        return users;
    }
}
